<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 
class Main_model extends Model {

	function getArticles($page, $showAll = false){
		if($showAll){
			$SQL="SELECT * 
				  FROM `articles` a
				  LEFT JOIN `pages` p ON a.`article_id` = p.`article_id`
				  WHERE p.`name`=\"".$page."\"";
		} else {
			$SQL="SELECT * 
				  FROM `articles` a
				  LEFT JOIN `pages` p ON a.`article_id` = p.`article_id`
				  WHERE p.`name`=\"".$page."\"
				  AND (
					p.`published`= \"Yes\"
					OR (
						p.`published`= \"Auto\"
						AND p.`published_start` < CURRENT_TIMESTAMP
						AND p.`published_end` > CURRENT_TIMESTAMP))";
		}
		$query = $this->db->query($SQL);
		if($query->num_rows > 0)
		{
			return $query->result();
		}
	}
	
	function getArticle($article_id, $page=""){
		if($page!=""){
			$SQL="SELECT * 
				  FROM `articles` a
				  LEFT JOIN `pages` p ON a.`article_id` = p.`article_id`
				  WHERE p.`name`=\"".$page."\"
				  AND a.`article_id`=\"".$article_id."\"";
		} else {
			$SQL="SELECT * 
				  FROM `articles` a
				  WHERE a.`article_id`=\"".$article_id."\"";
		}
			  
		$query = $this->db->query($SQL);
		
		if($query->num_rows > 0)
		{
			return $query->first_row();
		}
	}
	
	function getPromohoekId($id){
		$SQL="SELECT * FROM `promohoek` WHERE `id`=\"$id\"";
		$query=$this->db->query($SQL);
		if($query->num_rows > 0)
		{
			return $query->result();
		}
	}
	function getSuggestieId($id){
		$SQL="SELECT * FROM `suggesties` WHERE `id`=\"$id\"";
		$query=$this->db->query($SQL);
		if($query->num_rows > 0)
		{
			return $query->result();
		}
	}
	function getNieuwsId($id){
		$SQL="SELECT * FROM `nieuws` WHERE `id`=\"$id\"";
		$query=$this->db->query($SQL);
		if($query->num_rows > 0)
		{
			return $query->result();
		}
	}
	
	function getPromohoekHistory(){
		$now=date("Y-m-d H:i:s");
		$SQL="SELECT * FROM `promohoek` WHERE `tot` < \"$now\" ORDER BY `van`, `tot`";
		$query=$this->db->query($SQL);
		if($query->num_rows > 0)
		{
			return $query->result();
		}
	}
	function getSuggestieHistory(){
		$now=date("Y-m-d H:i:s");
		$SQL="SELECT * FROM `suggesties` WHERE `tot` < \"$now\" ORDER BY `van`, `tot`";
		$query=$this->db->query($SQL);
		if($query->num_rows > 0)
		{
			return $query->result();
		}
	}
	function getNieuwsHistory(){
		$now=date("Y-m-d H:i:s");
		$SQL="SELECT * FROM `nieuws` WHERE `tot` < \"$now\" ORDER BY `van`, `tot`";
		$query=$this->db->query($SQL);
		if($query->num_rows > 0)
		{
			return $query->result();
		}
	}
	
	function getPromohoekNow(){
		$now=date("Y-m-d H:i:s");
		$SQL="SELECT * FROM `promohoek` WHERE `van` < \"$now\" AND `tot` > \"$now\" ORDER BY `van`, `tot`";
		$query=$this->db->query($SQL);
		if($query->num_rows > 0)
		{
			return $query->result();
		}
	}
	function getSuggestieNow(){
		$now=date("Y-m-d H:i:s");
		$SQL="SELECT * FROM `suggesties` WHERE `van` < \"$now\" AND `tot` > \"$now\" ORDER BY `van`, `tot`";
		$query=$this->db->query($SQL);
		if($query->num_rows > 0)
		{
			return $query->result();
		}
	}
	function getNieuwsNow(){
		$now=date("Y-m-d H:i:s");
		$SQL="SELECT * FROM `nieuws` WHERE `van` < \"$now\" AND `tot` > \"$now\" ORDER BY `van`, `tot`";
		$query=$this->db->query($SQL);
		if($query->num_rows > 0)
		{
			return $query->result();
		}
	}
	
	function getPromohoekComing(){
		$now=date("Y-m-d H:i:s");
		$SQL="SELECT * FROM `promohoek` WHERE `van` > \"$now\" ORDER BY `van`, `tot`";
		$query=$this->db->query($SQL);
		if($query->num_rows > 0)
		{
			return $query->result();
		}
	}
	function getSuggestieComing(){
		$now=date("Y-m-d H:i:s");
		$SQL="SELECT * FROM `suggesties` WHERE `van` > \"$now\" ORDER BY `van`, `tot`";
		$query=$this->db->query($SQL);
		if($query->num_rows > 0)
		{
			return $query->result();
		}
	}
	function getNieuwsComing(){
		$now=date("Y-m-d H:i:s");
		$SQL="SELECT * FROM `nieuws` WHERE `van` > \"$now\" ORDER BY `van`, `tot`";
		$query=$this->db->query($SQL);
		if($query->num_rows > 0)
		{
			return $query->result();
		}
	}
	
	function savePromo($id,$van,$tot,$inhoud){
		$data = array('`van`' => $van, '`tot`' => $tot, '`inhoud`' => $inhoud);
		$where = "`id` = \"".$id."\""; 
		$this->db->query($this->db->update_string('`promohoek`', $data, $where));
	}
	function saveSuggestie($id,$van,$tot,$inhoud){
		$data = array('`van`' => $van, '`tot`' => $tot, '`inhoud`' => $inhoud);
		$where = "`id` = \"".$id."\""; 
		$this->db->query($this->db->update_string('`suggesties`', $data, $where));
	}
	function saveNieuws($id,$van,$tot,$inhoud){
		$data = array('`van`' => $van, '`tot`' => $tot, '`inhoud`' => $inhoud);
		$where = "`id` = \"".$id."\""; 
		$this->db->query($this->db->update_string('`nieuws`', $data, $where));
	}
	
	function newPromo($van,$tot,$inhoud){
		$data = array('`van`' => $van, '`tot`' => $tot, '`inhoud`' => $inhoud);
		$this->db->query($this->db->insert_string('`promohoek`', $data));
	}
	function newSuggestie($van,$tot,$inhoud){
		$data = array('`van`' => $van, '`tot`' => $tot, '`inhoud`' => $inhoud);
		$this->db->query($this->db->insert_string('`suggesties`', $data));
	}
	function newNieuws($van,$tot,$inhoud){
		$data = array('`van`' => $van, '`tot`' => $tot, '`inhoud`' => $inhoud);
		$this->db->query($this->db->insert_string('`nieuws`', $data));
	}
	
	function deletePromo($id){
		$SQL="DELETE FROM `promohoek` WHERE `id`=\"".$id."\"";
		$this->db->query($SQL);
	}
	function deleteSuggestie($id){
		$SQL="DELETE FROM `suggesties` WHERE `id`=\"".$id."\"";
		$this->db->query($SQL);
	}
	function deleteNieuws($id){
		$SQL="DELETE FROM `nieuws` WHERE `id`=\"".$id."\"";
		$this->db->query($SQL);
	}
	
	
	function savePage($id,$inhoud,$dispDate){
		$data = array('`inhoud`' => $inhoud, '`dispDate`'=> $dispDate);
		$where = "`id` = \"".$id."\""; 
		$this->db->query($this->db->update_string('`inhoud`', $data, $where));
	}
	function saveWijnInfo($id,$inhoud) {
		$data = array('`wijninfo`' => $inhoud);
		$where = "`id` = \"".$id."\""; 
		$this->db->query($this->db->update_string('`kaassoorten`', $data, $where));
	}
	function saveProductInfo($id,$inhoud) {
		$data = array('`productInfo`' => $inhoud);
		$where = "`id` = \"".$id."\""; 
		$this->db->query($this->db->update_string('`categories`', $data, $where));
	}
	
	function getBannerText(){
		$result = $this->getPromohoekNow();
		if($result!=NULL){
			return $result[0]->inhoud;
		} else {
			return "";
		}
	}
	function getSuggesties(){
		$result = $this->getSuggestieNow();
		if($result!=NULL){
			return $result;
		} else {
			return "";
		}
	}
	function getNieuws(){
		$result = $this->getNieuwsNow();
		if($result!=NULL){
			return $result;
		}
	}
	function getHistimages(){
		$SQL="SELECT * FROM `histimages` ORDER BY `order`";
		$query=$this->db->query($SQL);
		
		if($query!=NULL){
			return $query->result();
		} else {
			return "";
		}
	}
	
	function imgtodb($files){
		$i=0;
		foreach($files as $file){
			$data = array('`image`' => trim($file), '`info`' => "", '`order`' => $i);
			$this->db->query($this->db->insert_string('`histimages`', $data));
			$i++;
		}
	}
	
	function addproduct($cat,$item){
		$data = array('`naam`' => $item, '`category_ID`' => $cat, '`published`' => "1");
		$this->db->query($this->db->insert_string('`producten`', $data));
	}
	function addkaas($cat,$item){
		$data = array('`naam`' => $item, '`kaas_id`' => $cat, '`published`' => "1");
		$this->db->query($this->db->insert_string('`kazen`', $data));
	}
	function deleteproduct($id){
		$SQL="DELETE FROM `producten` WHERE `id`=\"".$id."\"";
		$this->db->query($SQL);
	}
	function deletekaas($id){
		$SQL="DELETE FROM `kazen` WHERE `id`=\"".$id."\"";
		$this->db->query($SQL);
	}
}